//
//  QYGNetRequestModel.m
//  wawaji_ios
//
//  Created by 云+物娱 on 2017/12/7.
//  Copyright © 2017年 yiguo qi. All rights reserved.
//

#import "QYGNetRequestModel.h"
#import "CMDollListModel.h"
#import "QYGMachineItemModel.h"
#import "QYGGameModel.h"
#import "QYGUserInfoModel.h"
#import "QYGCoinRecordModel.h"
#import "QYGPaymentModel.h"
#import "QYGGameRecordModel.h"
#import "QYGConfigModel.h"
#import "QYGMainCellFrameModel.h"


@implementation QYGNetRequestModel



/*
 * 账号登录
 */
+ (void)postLoginWithAccountUserName:(NSString *)username
                            password:(NSString *)password
                           Completed:(void (^)(QYGUserInfoModel *userInfo))completed
                     WithErrorCodeBlock:(ErrorCodeBlock)errorCodeBlock{
    [SVProgressHUD show];
    NSString *url = Account_Login_Url;
    
    NSDictionary *parameter = @{@"username" : username,
                                @"password" : password};
    [NetRequestClass NetRequestPostWithRequestURL:url WithParameter:parameter WithReturnValutBlock:^(id returanValue) {
        
        [SVProgressHUD dismiss];
        if(returanValue == nil) return;
        QYGUserInfoModel *userInfoModel = [QYGUserInfoModel mj_objectWithKeyValues:returanValue[@"data"]];
        completed(userInfoModel);
        
    } WithErrorCodeBlock:^(NSError *error) {
        errorCodeBlock(error);
    }];
}

/*
 *  进入、退出房间
 */
+ (void)postEnterOrExitRoomWithDeviceId:(NSInteger)deviceId
                                 Action:(RoomActionType)roomActionType
                              completed:(void (^)(BOOL isSuccess))completed
                     WithErrorCodeBlock:(ErrorCodeBlock)errorCodeBlock{
    NSString *url = Room_Action_Url(deviceId);
    NSDictionary *parameter = @{@"action" : roomActionType == RoomActionTypeEnter ? @"enter" : @"exit"};
    [NetRequestClass NetRequestPostWithRequestURL:url WithParameter:parameter WithReturnValutBlock:^(id returanValue) {
        completed(YES);
    } WithErrorCodeBlock:^(NSError *error) {
        errorCodeBlock(error);
        
    }];
}
/*
 * 获取配置信息
 */
+ (void)getConfigCompleted:(void (^)(QYGConfigModel *configDict))completed
              WithErrorCodeBlock:(ErrorCodeBlock)errorCodeBlock{
    [NetRequestClass NetRequestGetWithRequestURL:Config_Url WithParameter:nil WithRetuanValueBlock:^(id returanValue) {
        QYGConfigModel *configModel = [QYGConfigModel mj_objectWithKeyValues:returanValue[@"data"]];
        completed(configModel);
    } WithErrorCodeBlock:^(NSError *errorCode) {
        errorCodeBlock(errorCode);
        
    }];
}

/*
 *  交易单号查询
 */
+ (void)getCheckPaymentWithPay_id:(NSInteger)pay_id
                     completed:(void (^)(QYGPaymentModel *paymentModel))completed
            WithErrorCodeBlock:(ErrorCodeBlock)errorCodeBlock{
    NSString *url = Check_Payments_Url(pay_id);
    
    [SVProgressHUD show];
    [NetRequestClass NetRequestGetWithRequestURL:url WithParameter:nil WithRetuanValueBlock:^(id returanValue) {
        
        [SVProgressHUD dismiss];
        if(returanValue == nil) return;
        QYGPaymentModel *paymentModel = [QYGPaymentModel mj_objectWithKeyValues:returanValue[@"data"]];
        completed(paymentModel);
    } WithErrorCodeBlock:^(NSError *error) {
        
        errorCodeBlock(error);
    }];
}

/*
 *  获取支付参数配置
 */
+ (void)getPayParamsWithPay_id:(NSInteger)pay_id
                        completed:(void (^)(QYGPreparamModel *prePaymentModel))completed
               WithErrorCodeBlock:(ErrorCodeBlock)errorCodeBlock{
    NSString *url = Pay_Params_Url(pay_id);
    
    [NetRequestClass NetRequestGetWithRequestURL:url WithParameter:nil WithRetuanValueBlock:^(id returanValue) {
        
        if(returanValue == nil) return;
        QYGPreparamModel *prePayModel = [QYGPreparamModel mj_objectWithKeyValues:returanValue[@"data"]];
        completed(prePayModel);
    } WithErrorCodeBlock:^(NSError *error) {
        
        errorCodeBlock(error);
    }];
}
/*
 *  生成支付交易单号(模拟 )
 */
+ (void)postPaymentWithCoin_id:(NSInteger)coin_id
                    completed:(void (^)(QYGPaymentModel *paymentModel))completed
           WithErrorCodeBlock:(ErrorCodeBlock)errorCodeBlock{
    NSString *url = [QYGKeychain payType] ? Payments_Fake_Url : Payments_Url; //;
    NSDictionary *parameter = @{@"coin_id" : @(coin_id)};
    [NetRequestClass NetRequestPostWithRequestURL:url WithParameter:parameter WithReturnValutBlock:^(id returanValue) {
        if(returanValue == nil) return;
        QYGPaymentModel *paymentModel = [QYGPaymentModel mj_objectWithKeyValues:returanValue[@"data"]];
        completed(paymentModel);
    } WithErrorCodeBlock:^(NSError *error) {
        errorCodeBlock(error);
    }];
}
/*
 * 微信登录
 */
+ (void)getWechatUserInfoWithCode:(NSString *)code
                       completed:(void (^)(QYGUserInfoModel *userInfo))completed
              WithErrorCodeBlock:(ErrorCodeBlock)errorCodeBlock{
    [SVProgressHUD show];
    NSString *url = User_Login_Url;
    NSDictionary *parameter = @{@"code" : code};
    
    [NetRequestClass NetRequestPostWithRequestURL:url WithParameter:parameter WithReturnValutBlock:^(id returanValue) {
        [SVProgressHUD dismiss];
        if(returanValue == nil) return;
        QYGUserInfoModel *userInfoModel = [QYGUserInfoModel mj_objectWithKeyValues:returanValue[@"data"]];
        completed(userInfoModel);
        
    } WithErrorCodeBlock:^(NSError *error) {
        errorCodeBlock(error);
    }];
}
/*
 * 娃娃币充值列表
 */
+ (void)getCoinsListCompleted:(void (^)(NSArray *coinListArr))completed
            WithErrorCodeBlock:(ErrorCodeBlock)errorCodeBlock{
    NSString *url = Coins_List_Url;
    [NetRequestClass NetRequestGetWithRequestURL:url WithParameter:nil WithRetuanValueBlock:^(id returanValue) {
       
        NSArray *coinListArr = [QYGCoinListModel mj_objectArrayWithKeyValuesArray:returanValue[@"data"]];
        completed(coinListArr);
    } WithErrorCodeBlock:^(NSError *errorCode) {
        errorCodeBlock(errorCode);
    }];
}
/*
 * 娃娃币流水记录
 */
+ (void)getCoinRecordsWithPage:(NSInteger)page
                          per_page:(NSInteger)per_page
                         Completed:(void (^)(NSArray *coinRecordArr))completed
          WithErrorCodeBlock:(ErrorCodeBlock)errorCodeBlock{
    
    NSString *url = [NSString stringWithFormat:@"%@?page=%ld&per_page=%ld",Coin_Records_Url,page,per_page];
    [SVProgressHUD show];
    [NetRequestClass NetRequestGetWithRequestURL:url WithParameter:nil WithRetuanValueBlock:^(id returanValue) {
        [SVProgressHUD dismiss];
        if(returanValue == nil) return;
        NSArray *noticeEntityArr = [QYGCoinRecordModel mj_objectArrayWithKeyValuesArray:returanValue[@"data"]];
        completed(noticeEntityArr);
    } WithErrorCodeBlock:^(NSError *errorCode) {
        errorCodeBlock(errorCode);
    }];
}

/*
 * 娃娃币总充值数量
 */
+ (void)getCoinTotalPaymentsCompleted:(void (^)(NSInteger totalCoin))completed
                   WithErrorCodeBlock:(ErrorCodeBlock)errorCodeBlock{
    
    NSString *url = Coin_Payments_Total_Url;
    [NetRequestClass NetRequestGetWithRequestURL:url WithParameter:nil WithRetuanValueBlock:^(id returanValue) {
        [SVProgressHUD dismiss];
        if(returanValue == nil) return;
        NSDictionary *dict = returanValue[@"data"];
        NSInteger totalCoin = [dict[@"coin"] integerValue];
        completed(totalCoin);
    } WithErrorCodeBlock:^(NSError *errorCode) {
        errorCodeBlock(errorCode);
    }];
}
/*
 * 娃娃币充值记录
 */
+ (void)getCoinPaymentsWithPage:(NSInteger)page
                      per_page:(NSInteger)per_page
                     Completed:(void (^)(NSArray *coinRecordArr))completed
            WithErrorCodeBlock:(ErrorCodeBlock)errorCodeBlock{
    
    NSString *url = [NSString stringWithFormat:@"%@?page=%ld&per_page=%ld",Coin_Payments_Url,page,per_page];
    [SVProgressHUD show];
    [NetRequestClass NetRequestGetWithRequestURL:url WithParameter:nil WithRetuanValueBlock:^(id returanValue) {
        [SVProgressHUD dismiss];
        if(returanValue == nil) return;
        NSArray *noticeEntityArr = [QYGCoinRecordModel mj_objectArrayWithKeyValuesArray:returanValue[@"data"]];
        completed(noticeEntityArr);
    } WithErrorCodeBlock:^(NSError *errorCode) {
        errorCodeBlock(errorCode);
    }];
}
/*
 * 退出登录
 */
+ (void)getLoginOutCompleted:(void (^)(BOOL isSuccess))completed
          WithErrorCodeBlock:(ErrorCodeBlock)errorCodeBlock{
    [SVProgressHUD show];
    NSString *url = User_Logout_Url;
    [NetRequestClass NetRequestPostWithRequestURL:url WithParameter:nil WithReturnValutBlock:^(id returanValue) {
        [SVProgressHUD dismiss];
        completed(YES);
        
    } WithErrorCodeBlock:^(NSError *error) {
        errorCodeBlock(error);
    }];
  
}
/*
 * 用户信息
 */
+ (void)getUserInfoCompleted:(void (^)(QYGUserInfoModel *userInfo))completed
          WithErrorCodeBlock:(ErrorCodeBlock)errorCodeBlock{
    
    NSString *url = UserInfo_Url;
    [SVProgressHUD show];
    [NetRequestClass NetRequestGetWithRequestURL:url WithParameter:nil WithRetuanValueBlock:^(id returanValue) {
        [SVProgressHUD dismiss];
        if(returanValue == nil) return;
        QYGUserInfoModel *userInfoModel = [QYGUserInfoModel mj_objectWithKeyValues:returanValue[@"data"]];
        completed(userInfoModel);
    } WithErrorCodeBlock:^(NSError *errorCode) {
        errorCodeBlock(errorCode);
    }];
}
/*
 * 我的娃娃记录
 */
+ (void)getMineDollRecordsWithPage:(NSInteger)page
                          per_page:(NSInteger)per_page
                         Completed:(void (^)(NSArray *dollRecordArr))completed
         WithErrorCodeBlock:(ErrorCodeBlock)errorCodeBlock{
    [SVProgressHUD show];
    NSString *url = [NSString stringWithFormat:@"%@?page=%ld&per_page=%ld",Mine_Doll_Url,page,per_page];
    [NetRequestClass NetRequestGetWithRequestURL:url WithParameter:nil WithRetuanValueBlock:^(id returanValue) {
        [SVProgressHUD dismiss];
        NSArray *dollRecordArr = [QYGGameRecordModel mj_objectArrayWithKeyValuesArray:returanValue[@"data"]];
        completed(dollRecordArr);
    } WithErrorCodeBlock:^(NSError *errorCode) {
        errorCodeBlock(errorCode);
    }];
}
/*
 * 首页轮播图
 */
+ (void)getBannersCompleted:(void (^)(NSArray *bannerArr))completed
         WithErrorCodeBlock:(ErrorCodeBlock)errorCodeBlock{
    NSString *url = Grt_Banners_Url;
    [NetRequestClass NetRequestGetWithRequestURL:url WithParameter:nil WithRetuanValueBlock:^(id returanValue) {
        completed(returanValue[@"data"]);
    } WithErrorCodeBlock:^(NSError *errorCode) {
        errorCodeBlock(errorCode);
    }];
}

/*
 * 娃娃机抓取记录
 */
+ (void)getGameRecordWithDeviceId:(NSInteger)deviceId
                             page:(NSInteger)page
                     per_page:(NSInteger)per_page
                    Completed:(void (^)(NSArray *gameRecordArr))completed
           WithErrorCodeBlock:(ErrorCodeBlock)errorCodeBlock{
    [SVProgressHUD show];
    NSString *url = [NSString stringWithFormat:@"%@?page=%ld&per_page=%ld",Game_Records_Url(deviceId),page,per_page];
    [NetRequestClass NetRequestGetWithRequestURL:url WithParameter:nil WithRetuanValueBlock:^(id returanValue) {
        [SVProgressHUD dismiss];
        
        NSArray *gameRecordArr = [QYGGameRecordModel mj_objectArrayWithKeyValuesArray:returanValue[@"data"]];
        completed(gameRecordArr);
    } WithErrorCodeBlock:^(NSError *errorCode) {
        errorCodeBlock(errorCode);
    }];
}
/*
 * 我的游戏记录列表
 */
+ (void)getMineGameRecordWithPage:(NSInteger)page
                     per_page:(NSInteger)per_page
               operate_result:(NSInteger)operate_result
                    Completed:(void (^)(NSArray *mineGameArr))completed
           WithErrorCodeBlock:(ErrorCodeBlock)errorCodeBlock{
    [SVProgressHUD show];
    NSString *url = [NSString stringWithFormat:@"%@?page=%ld&per_page=%ld",Mine_Game_Records_Url,page,per_page];
    [NetRequestClass NetRequestGetWithRequestURL:url WithParameter:nil WithRetuanValueBlock:^(id returanValue) {
        [SVProgressHUD dismiss];
        NSArray *mineGameArr = [QYGGameRecordModel mj_objectArrayWithKeyValuesArray:returanValue[@"data"]];
        completed(mineGameArr);
    } WithErrorCodeBlock:^(NSError *errorCode) {
        errorCodeBlock(errorCode);
    }];
}
/*
 * 游戏操作
 */
+ (void)getGameOperateWithDevice_id:(NSInteger)device_id
                             action:(NSInteger)action
                          Completed:(void (^)(BOOL isSuccess))completed
                WithErrorCodeBlock:(ErrorCodeBlock)errorCodeBlock{
    NSString *url = [NSString stringWithFormat:@"%@?machine_id=%ld&action=%ld",Game_Operate_Url,device_id,action];
    [NetRequestClass NetRequestGetWithRequestURL:url WithParameter:nil WithRetuanValueBlock:^(id returanValue) {
        completed(YES);
    } WithErrorCodeBlock:^(NSError *errorCode) {\
        errorCodeBlock(errorCode);
    }];
    
}
/*
 * 游戏结果
 */
+ (void)getGameResultWithDevice_id:(NSInteger)device_id
                         completed:(void (^)(QYGGameResultModel *gameResultModel))completed
                WithErrorCodeBlock:(ErrorCodeBlock)errorCodeBlock{
    NSString *url = Game_Result_Url(device_id);
    [NetRequestClass NetRequestGetWithRequestURL:url WithParameter:nil WithRetuanValueBlock:^(id returanValue) {
        if(returanValue == nil) return;
        QYGGameResultModel *gameResultModel = [QYGGameResultModel mj_objectWithKeyValues:returanValue[@"data"]];
        completed(gameResultModel);
    } WithErrorCodeBlock:^(NSError *errorCode) {
        errorCodeBlock(errorCode);
    }];
    
}

/*
 *  开始游戏
 */
+ (void)postGameWithDevice_id:(NSInteger)device_id
                    completed:(void (^)(QYGGameMessageModel *gameModel))completed
           WithErrorCodeBlock:(ErrorCodeBlock)errorCodeBlock{
    NSDictionary *parameter = @{@"machine_id" : @(device_id)};
    [NetRequestClass NetRequestPostWithRequestURL:Game_Start_Url WithParameter:parameter WithReturnValutBlock:^(id returanValue) {
        if(returanValue == nil) return;
        QYGGameMessageModel *gameMessageModel = [QYGGameMessageModel mj_objectWithKeyValues:returanValue];
        completed(gameMessageModel);
    } WithErrorCodeBlock:^(NSError *error) {
        errorCodeBlock(error);
    }];
}
/*
 *  娃娃机详情
 */
+ (void)getDrollMachineItemDataWithDevice_id:(NSInteger)device_id
                              completed:(void (^)(QYGMachineItemModel *machineItemModel))completed
                     WithErrorCodeBlock:(ErrorCodeBlock)errorCodeBlock{
    NSString *url = Machine_Item_Url(device_id);
    [NetRequestClass NetRequestGetWithRequestURL:url WithParameter:nil WithRetuanValueBlock:^(id returanValue) {
        if(returanValue == nil) return;
        QYGMachineItemModel *machineItemModel = [QYGMachineItemModel mj_objectWithKeyValues:returanValue[@"data"]];
        completed(machineItemModel);
    } WithErrorCodeBlock:^(NSError *errorCode) {
        errorCodeBlock(errorCode);
    }];
    
}
/*
 *  娃娃机列表
 */
+ (void)getDrollListDataWithPage:(NSInteger)page
                      per_page:(NSInteger)per_page
                        completed:(void (^)(NSArray *noticeEntityArr))completed
               WithErrorCodeBlock:(ErrorCodeBlock)errorCodeBlock
{
    [SVProgressHUD show];
    NSString *url = [NSString stringWithFormat:@"%@?page=%ld&per_page=%ld",Machines_List_Url,page,per_page];
    [NetRequestClass NetRequestGetWithRequestURL:url WithParameter:nil WithRetuanValueBlock:^(id returanValue) {
        [SVProgressHUD dismiss];
        if(returanValue == nil) return;
        
        NSArray *noticeEntityArr = [CMDollListModel mj_objectArrayWithKeyValuesArray:returanValue[@"data"]];
       __block NSMutableArray *dollFArr = [NSMutableArray array];
        [noticeEntityArr enumerateObjectsUsingBlock:^(CMDollListModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            QYGMainCellFrameModel *mCellFModel = [[QYGMainCellFrameModel alloc] init];
            mCellFModel.dollListModel = obj;
            [dollFArr addObject:mCellFModel];
        }];
        completed(dollFArr);
    } WithErrorCodeBlock:^(NSError *errorCode) {
        errorCodeBlock(errorCode);
    }];
    
}

/*
 *  首页娃娃抓中列表
 */
+ (void)getNoticeListDataCompleted:(void (^)(NSArray *noticeArr))completed
                WithErrorCodeBlock:(ErrorCodeBlock)errorCodeBlock{
    NSString *url = HOME_Notices_List_Url;
    [NetRequestClass NetRequestGetWithRequestURL:url WithParameter:nil WithRetuanValueBlock:^(id returanValue) {
        if(returanValue == nil) return;
        
        NSArray *noticeArr = [QYGNoticeModel mj_objectArrayWithKeyValuesArray:returanValue[@"data"]];
        completed(noticeArr);
    } WithErrorCodeBlock:^(NSError *errorCode) {
        errorCodeBlock(errorCode);
    }];
}
@end
